﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Text;

using ComLib;
using ComLib.Web.Lib.Attributes;
using ComLib.Web.Modules.Widgets;
using ComLib.Web.Modules.Profiles;
using ComLib.Data;


namespace ComLib.CMS.Models.Widgets
{

    /// <summary>
    /// Users Widget
    /// </summary>    
    [Widget(Name = "Users", Author = "Kishore Reddy", Email = "kishore_reddy@codeplex.com", 
        IsCachable = true, IsEditable = true, SortIndex = 5, Version = "0.9.4.1", Path = "Widgets/Users")]
    public class Users : WidgetInstance
    {       
        /// <summary>
        /// Gets or sets the number of entries to show horizontally
        /// </summary>
        /// <value>The number of entries.</value>
        [Range(1, 40)]
        [PropertyDisplay(Name = "Number Of Entries Across", Order = 2, DefaultValue = "3", Description = "How many entries to display horizontally")]
        public int NumberOfEntriesAcross { get; set; }


        /// <summary>
        /// Gets or sets the total number of entries to get/display
        /// </summary>
        /// <value>The number of entries.</value>
        [Range(1, 200)]
        [PropertyDisplay(Name = "Number Of Entries", Order = 2, DefaultValue = "18", Description = "How many total entries to display")]
        public int NumberOfEntries { get; set; }


        /// <summary>
        /// Gets or sets the total number of entries to get/display
        /// </summary>
        /// <value>The number of entries.</value>
        [Range(0, 150)]
        [PropertyDisplay(Name = "Size", Order = 2, DefaultValue = "40", Description = "Size in pixels of the user profile thumbnail pic")]
        public int Size { get; set; }


        /// <summary>
        /// Renders itself
        /// </summary>
        public override bool IsSelfRenderable
        {
            get { return true; }
        }


        public override string DoRender()
        {
            IQuery<Profile> query = Query<Profile>.New().Where(p => p.IsGravatarEnabled).Is(true).Or(p => p.ImageRefId).MoreThan(0);
            PagedList<Profile> items = ComLib.Web.Modules.Profiles.Profile.Find(query, 1, this.NumberOfEntries);
            var buffer = new StringBuilder();
            buffer.Append("<div class=\"usercloud\"><table>");
            int size = Size;
            if (size == 0) size = 40;

            // This dispalys photos in columns of 3.
            EnumerableHelper.ForEachByCols(items.Count, NumberOfEntriesAcross, (startNdx, endNdx) =>
            {
                buffer.Append("<tr>");
                for (int ndx = startNdx; ndx <= endNdx; ndx++)
                {
                    ComLib.Web.Modules.Profiles.Profile profile = items[ndx];
                    string imageUrl = profile.GetImageUrl(true, size, true, "top", "border:0", true);
                    buffer.Append("<td><a href=\"/profile/DetailsByUser/" + profile.UserId + "\">" + imageUrl + "</a></td>" + Environment.NewLine);
                }
                buffer.Append("</tr>");
            });
            buffer.Append("</table></div>");
            string text = buffer.ToString();
            return text;
        }
    }
}
